
                /*********************************************
                **                                          **
                **                                          **
                **              commit Group                **
                **                                          **
                **                                          **
                *********************************************/
                /*********************************************
                **                                          **
                **         Author Jean-Marc Adamo           **
                **       Copyright (C) 1997-2008 JMA        **
                *********************************************/


/*
** execute with 6 processes
*/

#include "arch.h"
#include "A.utilities"

//user program

Coll_id cid1, cid2, cid3;

//on most homogenous systems, explicit setting 
//won't be necessary (try commenting the two
//setting lines, you should get the same values
//for cid1 and cid2).
void init(int, char **){
   cid1.set();
   cid2.set();
   fprintf(Sdf::trace, "node%d: cid1=%d, cid2=%d, cid3=%d\n",
           Sdf::PROC_ID, cid1.get(), cid2.get(), cid3.get());
   fflush(Sdf::trace);
}

class Root_proc: public Thread{

   public:

        Root_proc()
                : Thread((Tmf0_ptr)&Root_proc::body){}

        void body(){

            fprintf(Sdf::trace, "node %d, in Root_proc::starts\n", Sdf::PROC_ID);
            fflush(Sdf::trace);

//uncomment to get error detection and program abortion
//if(!Sdf::PROC_ID){
//          int rank1s[3] = {2, 3, 5};
//          Group *grp_incl1 = Sdf::incl(Sdf::COMML_WORLD, 3, rank1s);
//}else{
            int rank1s[3] = {1, 3, 5};
            Group *grp_incl1 = Sdf::incl(Sdf::COMML_WORLD, 3, rank1s);
//}

            int grp_incl1_size = grp_incl1->size();
            int grp_incl1_rank = grp_incl1->rank();

            int rank2s[3] = {0, 2, 4};
            Group *grp_incl2 = Sdf::incl(Sdf::COMML_WORLD, 3, rank2s);

            int grp_incl2_size = grp_incl2->size();
            int grp_incl2_rank = grp_incl2->rank();

            Group *grp_add = Sdf::add(grp_incl1, grp_incl2);

            int grp_add_size = grp_add->size();
            int grp_add_rank = grp_add->rank();

            CommL *comm = grp_incl1->commit(cid1, Sdf::COMML_WORLD);

            fprintf(Sdf::trace, "node %d, 1size=%d, 2size=%d, 3size=%d, 1rank=%d, 2rank=%d, 3rank=%d\n", 
                    Sdf::PROC_ID, 
                    grp_incl1_size, grp_incl2_size, grp_add_size,
                    grp_incl1_rank, grp_incl2_rank, grp_add_rank);
            fflush(Sdf::trace);

            delete comm;
            delete grp_incl2;

            fprintf(Sdf::trace, "node %d, in Root_proc::finishes\n", Sdf::PROC_ID);
            fflush(Sdf::trace);
        }
};
//end of user program

MAIN(Root_proc, init, Sdf::empty, Sdf::empty)

